<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);

class guestbook_admin extends admin {

	private $db;
  public $username;
  /**
   * 构造函数
   */
	public function __construct() {
		parent::__construct();
		//if (!module_exists(ROUTE_M)) showmessage(L('module_not_exists'));
		$this->username = param::get_cookie('admin_username');
		$this->db = pc_base::load_model('guestbook_model');
	}

  /**
   * 入口函数，显示新提交的留言列表
   * @return [type] [description]
   */
	public function init() {
		$sql = '';
		// $_GET['status'] = $_GET['status'] ? intval($_GET['status']) : 1;
		$sql = '`siteid`=\''.$this->get_siteid().'\' AND `status`=0';

		$page = max(intval($_GET['page']), 1);
		$data = $this->db->listinfo($sql, '`listorder` DESC', $page, $pagesize = 20);
		$pages = $this->db->pages;
		// $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=announce&c=admin_announce&a=add\', title:\''.L('announce_add').'\', width:\'700\', height:\'500\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('announce_add'));
		include $this->admin_tpl('guestbook_list');
	}

	/**
	 * 已回复留言列表
	 * @return [type] [description]
	 */
	public function replyed_list() {
		$sql = '';
		// $_GET['status'] = $_GET['status'] ? intval($_GET['status']) : 1;
		$sql = '`siteid`=\''.$this->get_siteid().'\' AND `status`=1';

		$page = max(intval($_GET['page']), 1);
		$data = $this->db->listinfo($sql, '`listorder` DESC', $page, $pagesize = 20);
		$pages = $this->db->pages;
		// $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=announce&c=admin_announce&a=add\', title:\''.L('announce_add').'\', width:\'700\', height:\'500\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('announce_add'));
		include $this->admin_tpl('guestbook_replyed');
	}

	/**
	 * 修改留言
	 * @return [type] [description]
	 */
	public function edit() {
		$_GET['gid'] = intval($_GET['gid']);
		if(!$_GET['gid']) showmessage(L('illegal_operation'));

		if(isset($_POST['dosubmit'])) {

			$_POST['guestbook'] = $this->check($_POST['guestbook'], 'edit');
			if($this->db->update($_POST['guestbook'], array('gid' => $_GET['gid']))) showmessage(L('announced_a'), HTTP_REFERER, '', 'edit');
		}
		else {
			$where = array('gid' => $_GET['gid']);
			$an_info = $this->db->get_one($where);

			pc_base::load_sys_class('form', '', 0);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$template_list = template_list($this->siteid, 0);
			foreach ($template_list as $k=>$v) {
				$template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
				unset($template_list[$k]);
			}
			$show_header = $show_validator = $show_scroll = 1;
			include $this->admin_tpl('guestbook_edit');
		}
	}

	public function reply(){
		$_GET['gid'] = intval($_GET['gid']);
		if(!$_GET['gid']) showmessage(L('illegal_operation'));

		//是则写入数据，否则调取数据显示
		if(isset($_POST['dosubmit'])) {
			$_POST['guestbook'] = $this->check($_POST['guestbook'], 'reply');
			if($this->db->update($_POST['guestbook'], array('gid' => $_GET['gid']))) showmessage(L('announced_a'), HTTP_REFERER, '', 'reply');
		}
		else{
			$where = array('gid' => $_GET['gid']);
			$an_info = $this->db->get_one($where);

			pc_base::load_sys_class('form', '', 0);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$template_list = template_list($this->siteid, 0);
			foreach ($template_list as $k=>$v) {
				$template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
				unset($template_list[$k]);
			}
			$show_header = $show_validator = $show_scroll = 1;
			$_username = param::get_cookie('admin_username');

			include $this->admin_tpl('guestbook_reply');
		}
	}

	/**
	 * 批量删除
	 * @param  integer $gid [description]
	 * @return [type]       [description]
	 */
	public function delete($gid = 0){
		if((!isset($_POST['gid']) || empty($_POST['gid'])) && !$gid) {
			showmessage(L('illegal_operation'));
		} else {
			if(is_array($_POST['gid']) && !$gid) {
				array_map(array($this, 'delete'), $_POST['gid']);
				showmessage(L('guestbook_deleted'), HTTP_REFERER);
			} elseif($gid) {
				$gid = intval($gid);
				$this->db->delete(array('gid' => $gid));
			}
		}
	}

	/**
	 * 单条通过审核
	 * @param  integer $gid [description]
	 * @return [type]       [description]
	 */
	public function passed() {
		$gid = intval($_GET['gid']);
		if ( $gid < 0 ){
			showmessage(L('illegal_operation'));
		}
		else{
			$this->db->update(array('passed' => $_GET['passed']), array('gid' => $gid));
			showmessage(L('operation_success'), HTTP_REFERER);
		}
	}

	/**
	 * 批量取消审核
	 * @param  integer $gid [description]
	 * @return [type]       [description]
	 */
	public function cancel_passed($gid = 0) {
		if((!isset($_POST['gid']) || empty($_POST['gid'])) && !$gid) {
			showmessage(L('illegal_operation'));
		} else {
			if(is_array($_POST['gid']) && !$gid) {
				array_map(array($this, 'cancel_passed'), $_POST['gid']);
				showmessage(L('cancel_passed'), HTTP_REFERER);
			} elseif($gid) {
				$gid = intval($gid);
				$this->db->update(array('passed' => $_GET['passed']), array('gid' => $gid));
				return true;
			}
		}
	}

	public function listorder(){
		if(isset($_POST['dosubmit'])) {
			foreach($_POST['listorders'] as $gid => $listorder) {
				$this->db->update(array('listorder'=>$listorder),array('gid'=>intval($gid)));
			}
			showmessage(L('operation_success'),HTTP_REFERER);
		}
	}
	/**
	 * 验证表单数据
	 * @param  		array 		$data 表单数组数据
	 * @param  		string 		$a 当表单为添加数据时，自动补上缺失的数据。
	 * @return 		array 		验证后的数据
	 */
	private function check($data = array(), $a = 'add') {
		switch ($a) {
			case 'add':
				# code...
				break;
			case 'edit':
				if($data['title']=='') showmessage(L('title_cannot_empty'));
				if($data['content']=='') showmessage(L('announcements_cannot_be_empty'));
				$data['addtime'] = strtotime($data['addtime']);
				break;
			case 'reply':
				if($data['replycontent']=='') showmessage(L('回复内容不能为空'));
				$data['status'] = 1;
				$data['replytime'] = strtotime($data['replytime']);
				break;
			default:
				# code...
				break;
		}

		return $data;
	}

}
?>
